#include<bits/stdc++.h>
using namespace std;

struct node
{
	int x, y, step;
};

int n, m;
char maze[110][110];
bool vis[110][110];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int cnt;
int qx, qy;

void bfs()
{
	queue<node> q;
	q.push({qx, qy, 0});
	vis[qx][qy] = 1;
	
	while(!q.empty())
	{
		node t = q.front();
		q.pop();
		if(maze[t.x][t.y] == 'T')
		{
			cnt = t.step;
			return ;
		}
		
		for(int i = 0;i < 4;i++)
		{
			int xx = t.x+dx[i];
			int yy = t.y+dy[i];
			if(!vis[xx][yy] && xx > 0 && xx <= n && yy > 0 && yy <= m && maze[xx][yy] != '#')
			{
				q.push({xx, yy, t.step+1});
				vis[xx][yy] = 1;
			}
		}
	}
	
}

int main()
{
	cin>>n>>m;
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= m;j++)
		{
			cin>>maze[i][j];
			if(maze[i][j] == 'S')
				qx = i, qy = j;
		}
	}
	bfs();
	cout<<cnt;
    return 0;
}

